package com.tuiyun.jxgl.action;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.transaction.annotation.Transactional;

import com.tuiyun.jxgl.bean.Student;
import com.tuiyun.jxgl.dao.StudentHome;

public class EditAction {
	
	private Student student;
	
	private String msg;
	@Transactional
	public String execute(){
		if(id != -1){
			//admin edits student
			Student s = sh.findById(id);
            s.setAddress(address);
            //s.setAge(age);
            //s.setGender(gender);
            s.setPhone(phone);
            s.setCarType(carType);
            //s.setIdcard(idcard);
            s.setRealName(realName);
            sh.attachDirty(s);
            setStudent(s);
		}else{
			//student edit itself
			Subject subject = SecurityUtils.getSubject();		
	        if (subject != null) {        	
	            Student s = (Student) subject.getPrincipal();
	            s.setAddress(address);
	            //s.setAge(age);
	            //s.setGender(gender);
	            s.setPhone(phone);
	            s.setCarType(carType);
	            sh.attachDirty(s);
	            setStudent(s);
	        }
		}
		
		msg = "编辑成功！";
		return "success";
	}
	
	private StudentHome sh;
	private int id = -1;
	private int age;
	private byte gender;
	private String phone;
	private String address;
	private String realName;
	private String idcard;
	private byte carType;
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public String getIdcard() {
		return idcard;
	}
	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}
	public byte getCarType() {
		return carType;
	}
	public void setCarType(byte carType) {
		this.carType = carType;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public byte getGender() {
		return gender;
	}
	public void setGender(byte gender) {
		this.gender = gender;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public StudentHome getSh() {
		return sh;
	}
	public void setSh(StudentHome sh) {
		this.sh = sh;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
}
